/**
 * Created by z on 2016/11/3.
 */
;$(function () {
// 管道
    var $pipeline = $('#JQ_pipeline').select3();
// 管段
    var $segment = $('#pipeSegmentArr').select3({
        title: '管段'
    });

// 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment}
    });
// 终点位置
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment, 'beginSignId': $begin}
    });
// 初始化日期控件
    var $date_01 = $('#JQ_input_date_01');
    var $date_02 = $('#JQ_input_date_02');

//触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin', true);
                var end = DyyBase.getPos(dataset, 'end', true);
                $begin.asset(begin);
                $end.asset(end);
            });

        } else {
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
// 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin', true);
                var end = DyyBase.getPos(dataset, 'end', true);
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });
// 搜索条件
    var filter = {
        // 获得表单项
        getCondition: function (data) {
            var filter = {};
            filter.mode = true;
            if (data) {
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }

            //if(!DyyBase.search){
            //    return filter;
            //}
            // 管道id
            filter.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            //管道名称
            filter.pipeName = function () {
                var v = $pipeline.val('text');
                if (v) {
                    return v;
                } else {
                    $pipeline.element.TIP({content: '管道名称不能为空!'});
                    filter.mode = false;
                }
                return '';
            }();
            filter.startDate = function () {
                var v1 = $date_01.val();
                var v2 = $date_02.val();
                if (v2) {
                    if (!(Dyy.checkDateLate(v1, v2))) {
                        $date_02.TIP({content: '结束时间须晚于开始时间!'});
                        filter.mode = false;
                    }
                    return v2;
                }
                return '';
            }();
            filter.endDate = function () {
                var v = $date_02.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            //起点绝对距离
            filter.fromMileage = function () {
                var v = $begin.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            };
            //终点绝对距离
            filter.toMileage = function () {
                var v = $end.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            };
            return filter;
        },
        // 执行查询后的筛选对象
        getExportFilter: function () {
            return filter.getCondition();
        }
    };
//查询按钮
    $('#circleNull').show();
    $('#JQ_hacg').html('<img src="'+$('#JQ_hacg').attr('data-empty')+'">');
    $("#JQ_search").on('click', function () {
        var param = filter.getCondition();
        if (param.mode) {
            DyyBase.ajax(DyyBase.root_path + "/riskMgr/hca/statistics.json", param, function (dataset) {
                var data = new Array();
                data = dataset.result.countHacGradeList;

                var data1 = d3Charts.formartX1X2Y(dataset.result.queryHacGradeList);
                var color = ["#fec400", "#37a4dd", "#5dc3c7"];
                var flag = 0;
                for (var i = 0; i < data.length; i++) {
                    if (data[i] > 0) {
                        flag = 1;
                    }
                }
                if (flag == 1) {

                    if (data != '') {
                        var arr1 = [];
                        var arr2 = [];
                        for (var j = 0; j < data.length; j++) {
                            if (data[j] == 0) {

                            } else {
                                arr1.push(data[j]);
                                arr2.push(color[j]);
                            }
                        }
                        $('#circleChart').loadPowerCircle(arr1, arr2);
                        $('#circleNull').hide();
                        $('#lsk-charts').css({display: 'block'})
                    } else {
                        $('#lsk-charts').css({display: 'none'})
                    }

                } else {
                    //$('#circleChart').hide();
                    $('#circleNull').show();
                    $('#circleChart').html('');
                    $('#lsk-charts').css({display: 'none'})
                }
                if (data1.store.length) {
                    d3Charts.segment({
                        container: 'JQ_hacg',
                        color: ['#4a9ddf'],
                        data: data1,
                        tip: {
                            show: true,
                            xtag: '绝对距离(m)',
                            ytag: '等级'
                        },
                        xAxis: {
                            ticks: 5
                        },
                        yAxis: {
                            data: [0, 3],
                            ticks: 3,
                            render: function (d) {
                                switch (d) {
                                    case 1:
                                        return 'Ⅰ级';
                                        break;
                                    case 2:
                                        return 'Ⅱ级';
                                        break;
                                    case 3:
                                        return 'Ⅲ级';
                                        break;
                                    default:
                                        break;
                                }
                            }
                        },
                        pointText: {
                            circle: true
                        },
                        layout: {
                            margin: {
                                left: 40,
                                right: 30,
                                top: 10,
                                bottom: 20
                            },
                            xtag: '',
                            ytag: '',
                            ygrid: true
                        }
                    });
                    //$("#lsk-d").css("display","none")
                } else {
                    $('#JQ_hacg').html('<img src="'+$('#JQ_hacg').attr('data-empty')+'">');
                    //$("#lsk-d").css("display","none")

                }
            });
        }


    });

//导出
    $('#JQ_export_btn_01').click(function () {
        var param = filter.getCondition;
        if (param instanceof Function) {
            param = param();
        }
        $(this).EXPT({
            type: 'name',
            name: param.pipeName + '高后果区统计图',
            yesBtnAct: function (callback, fname) {
                callback();
                canvasPdf.exportHtmlPdf({
                    dom: $('#JQ_pdf_area'),
                    server: DyyBase.root_path + '/comm/downImgPdf.json',
                    param: {
                        fileName: fname,
                        name: param.pipeName + '高后果区统计图'
                    }
                });
            }
        });

        //    canvasPdf.exportHtmlPdf({
        //        dom: $('#JQ_pdf_area'),
        //        server: DyyBase.root_path + '/comm/downImgPdf.json',
        //        param: {
        //            name: '高后果区等级统计'
        //        }
        //    });
    });

})

//饼状统计
/**
 * Created by xiangyufang on 2016/11/4.
 */
;
$(function () {
    $.fn.extend({
        loadPowerCircle: function (data, colorData) {
            //console.log("DS");
            var self = this;
            self.empty();
            var width = self.width();
            var height = self.height();
            var outerRadius = 100;
            var innerRadius = 50;
            var len = data.length;

            //计算数组的总大小
            var count = 0;
            for (var i = 0; i < len; i++) {
                count += data[i];
            }
            if (count == 0) {
                self.hide();
                $('#circleNull').show();
            } else {
                // 插入svg
                var svg = d3.select("#circleChart")
                    .append("svg")
                    .attr("width", width)
                    .attr("height", height);

                // 对数据进行布局
                var pie = d3.pie();
                var piedata = pie(data);

                var colorOrdinal = d3.scaleOrdinal()
                    .domain(d3.range(colorData.length))
                    .range(colorData);

                var arc = d3.arc()
                    .innerRadius(innerRadius)
                    .outerRadius(outerRadius);

                var arcs = svg.selectAll("g")
                    .data(piedata)
                    .enter()
                    .append("g")
                    .attr("transform", "translate(" + width / 2 + "," + 100 + ")");

                arcs.append("path")
                    .attr("fill", function (d, i) {
                        return colorOrdinal(i);
                    })
                    .attr("d", function (d) {
                        return arc(d);
                    });

                arcs.append("text")
                    .attr("transform", function (d) {
                        return "translate(" + arc.centroid(d) + ")";
                    })
                    .attr("text-anchor", "middle")
                    .attr("fill", "white")
                    .text(function (d) {
                        var val = (d.data / count * 100).toFixed(2);
                        return val + "%";
                    });
            }
        }
    })
});

